/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.rest;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.anniversary.domain.Praise;
import me.zhengjie.modules.anniversary.repository.PraiseRepository;
import me.zhengjie.modules.anniversary.service.PraiseService;
import me.zhengjie.modules.anniversary.service.dto.PraiseQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author wuhui
* @date 2020-09-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "点赞管理")
@RequestMapping("/api/praise")
public class PraiseController {

    private final PraiseService praiseService;
    @Autowired
    private PraiseRepository praiseRepository;

    @Log("查询点赞数")
    @ApiOperation("查询点赞数")
    @GetMapping(value = "/selectPraise")
//    @PreAuthorize("@el.check('praise:list')")
    @AnonymousAccess
    public String selectPraise(@RequestParam("praiseId") String praiseId) throws IOException {
        int num = praiseRepository.findPraise(praiseId);
        return JSONObject.toJSON(num).toString();
    }

    @Log("查询点赞人信息")
    @ApiOperation("查询点赞人信息")
    @GetMapping(value = "/selectMessageUser")
//    @PreAuthorize("@el.check('praise:list')")
    @AnonymousAccess
    public Object selectMessageUser(@RequestParam("messageId") String messageId) throws IOException {
        List<Praise> messageUser = praiseRepository.findMessageUser(messageId);
        return messageUser;
    }

    @PostMapping("/insertPraise")
    @Log("新增点赞人1")
    @ApiOperation("新增点赞人1")
//    @PreAuthorize("@el.check('message:add')")
    @AnonymousAccess
    public String insertPraise(HttpServletRequest request){
        //定义一个contents的String变量
        String contents = null;
        //new一个map对象
        Map<String, Integer> map = new HashMap<String, Integer>();
        //new一个Message对象
        Praise praise = new Praise();
        String praiseId = request.getParameter("praiseId");
        String userId = request.getParameter("userId");
        String praiseName = request.getParameter("praiseName");
        praise.setPraiseId(Long.valueOf(praiseId));
        praise.setUserId(Long.valueOf(userId));
        praise.setPraiseName(praiseName);
        System.out.println("+++++praise:"+praise);
        int i = praiseRepository.insertPraise(praise);
        System.out.println("+++++contents:"+i);
        contents = String.valueOf(i);
        return contents;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('praise:list')")
    public void download(HttpServletResponse response, PraiseQueryCriteria criteria) throws IOException {
        praiseService.download(praiseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询点赞")
    @ApiOperation("查询点赞")
    @PreAuthorize("@el.check('praise:list')")
    public ResponseEntity<Object> query(PraiseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(praiseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增点赞")
    @ApiOperation("新增点赞")
    @PreAuthorize("@el.check('praise:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Praise resources){
        return new ResponseEntity<>(praiseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改点赞")
    @ApiOperation("修改点赞")
    @PreAuthorize("@el.check('praise:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Praise resources){
        praiseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除点赞")
    @ApiOperation("删除点赞")
    @PreAuthorize("@el.check('praise:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        praiseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
